class Solution {
public:
    void duplicateZeros(vector<int>& arr) {
        int cur = 0;
        int dest = -1;
        while (cur < arr.size())
        {
            if (arr[cur] != 0)
                dest++;
            else
                dest += 2;

            if (dest >= arr.size() - 1)
                break;

            cur++;
        }

        if (dest == arr.size())
        {
            arr[arr.size() - 1] = 0;
            dest -= 2;
            cur--;
        }

        while (cur >= 0)
        {
            if (arr[cur] != 0)
            {
                arr[dest] = arr[cur];
                dest--;
            }
            else
            {
                arr[dest] = 0;
                dest--;
                arr[dest] = 0;
                dest--;
            }
            cur--;
        }
    }
};